package com.bw.zg2.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.zg2.base.BaseResult
import com.bw.zg2.base.BaseViewModel
import com.bw.zg2.bean.GoodsDetailBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *
 * @ClassName: GoodsDetailViewModel
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/25 3:30 PM
 * @Version: 1.0
 */
class GoodsDetailViewModel : BaseViewModel() {

    val liveDataGoodsDetail = MutableLiveData<BaseResult<GoodsDetailBean>>()
    val liveDataGoodsCart = MutableLiveData<BaseResult<String>>()
    val liveDataDeleteGoodsCart = MutableLiveData<BaseResult<String>>()

    /**
     * 获取商品详情
     * @param goodsId Int
     */
    fun getDetail(goodsId: Int) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                liveDataGoodsDetail.postValue(apiService.getGoodsDetail(goodsId))
            }

        }
    }

    /**
     * 添加购物车
     * @param map Map<String, Int>
     */
    fun addCart(map: Map<String,Int>) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                liveDataGoodsCart.postValue(apiService.addGoodsCar(map))
            }

        }
    }

    /**
     * 删除购物车
     * @param map Map<String, Int>
     */
    fun deleteCart(map: Map<String,String>) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                liveDataDeleteGoodsCart.postValue(apiService.deleteGoodsCar(map))
            }

        }
    }

    /**
     * 删除购物车
     * @param map Map<String, Int>
     */
    fun gateway(map: Map<String,String>) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                liveDataDeleteGoodsCart.postValue(apiService2.gateway(map))
            }

        }
    }
}